package com.moyu.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moyu.admin.constant.AppConstants;
import com.moyu.admin.service.IUserInfoService;
import com.moyu.admin.shiro.PasswordHelper;
import com.moyu.data.dao.SysUserLoginMapper;
import com.moyu.data.dao.UserInfoMapper;
import com.moyu.data.domain.SysUserLogin;
import com.moyu.data.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author makun
 * @since 2019-02-22
 */
@Service
@Transactional
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

  @Autowired
  private SysUserLoginMapper userLoginMapper;

  @Autowired
  private UserInfoMapper userInfoMapper;

  @Override
  public void addUser(UserInfo userInfo) {
    SysUserLogin userLogin = new SysUserLogin();
    userLogin.setUsername(userInfo.getUsername());
    userLogin.setPassword(AppConstants.DEFAULT_PASSWORD);
    new PasswordHelper().encryptPassword(userLogin);
    userLoginMapper.insert(userLogin);

    userInfo.setUserId(userLogin.getId());
    userInfoMapper.insert(userInfo);
  }

  @Override
  public UserInfo getByUserId(long userId) {
    QueryWrapper qw = new QueryWrapper();
    qw.eq("user_id", userId);
    return userInfoMapper.selectOne(qw);
  }

}
